/**
 * Created by Richard Radlherr.
 * Date: 15.07.2006 18:43
 */

using System;
using System.Collections.Generic;
using CsGL;
using CsGL.OpenGL;
using TagTool.Core;
using TagTool.OpenGL;
using TagTool.GraphicTypes;
using System.Timers;

namespace TagTool.Nodes.Graphic
{

	public class Cursor : BaseNode
	{
		private Timer timer = new Timer();
		private InputSlot size;
		private InputSlot position;
		private InputSlot inColor;
		private InputSlot timeout;
		
		private OutputSlot shape;

		private Primitive rectangle = new Primitive();
		private Primitive cross = new Primitive();
		private Shape none = new NullShape();
		private Color color = new Color(1, 1, 1, 1);
		
		public Cursor() : base()
		{
			timer.Elapsed += new ElapsedEventHandler(this.OnTimer);
			Name = "Cursor";
			Initialize();
			
			size = new InputSlot(this, "Size", (double)0.0);
			size.ParseCallback = ParseHelper.ParseDouble;
			
			shape = new OutputSlot(this, "Shape", typeof(TagTool.GraphicTypes.Shape));
			shape.Value = cross;
			
			position = new InputSlot(this, "Position", new PointD());
			position.ParseCallback = ParseHelper.ParseDouble;
			
			inColor = new InputSlot(this, "Color", color);
			inColor.ParseCallback = ParseHelper.ParseColor;
			
			timeout = new InputSlot(this, "Timout", 4000.0);
			timeout.ParseCallback = ParseHelper.ParseDouble;
		}
		
		private void OnTimer(Object sender, EventArgs e)
		{
			shape.Value = none;
		}
		
		public override void Initialize() {
			none.Dispose();
			none = new NullShape();
			none.Update();
			
			rectangle.Dispose();
			rectangle = new Primitive();
			rectangle.Add(new ColoredPoint(-1, 1, 0, color, 0 ));
			rectangle.Add(new ColoredPoint(1, 1, 0, color, 0 ));
			rectangle.Add(new ColoredPoint(1, -1, 0, color, 0 ));
			rectangle.Add(new ColoredPoint(-1, -1, 0, color, 0 ));
			rectangle.PrimitiveType = PrimitiveType.QUADS;
			rectangle.Update();
			
			cross.Dispose();
			cross = new Primitive();
			cross.Add(new ColoredPoint(0, -.3, 0, new Color(1, 1, 1, 1), 2 ));
			cross.Add(new ColoredPoint(0, .3, 0, new Color(1, 1, 1, 1), 2 ));
			
			cross.Add(new ColoredPoint(-.3, 0, 0, new Color(1, 1, 1, 1), 2 ));
			cross.Add(new ColoredPoint(.3, 0, 0, new Color(1, 1, 1, 1), 2 ));
			cross.PrimitiveType = PrimitiveType.LINES;
			cross.Update();
		}
		
		public override void Update(Object sender)
		{
			timer.Interval = (double)timeout.Value;

			if ((double)size.Value == 0)
			{
				cross.Scale.X = 1;
				cross.Scale.Y = 1;
				cross.Scale.Z = 1;
				cross.Translate = (PointD)position.Value;
				shape.Value = cross;
			}
			else
			{
				rectangle.Scale.X = (double)size.Value;
				rectangle.Scale.Y = (double)size.Value;
				rectangle.Scale.Z = (double)size.Value;
				rectangle.Translate = (PointD)position.Value;
				shape.Value = rectangle;
			}
			
			rectangle.SetColor((Color)inColor.Value);
			cross.SetColor((Color)inColor.Value);
			
			timer.Stop();
			timer.Start();
			CastTrigger();
		}
		
		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot Timeout
		{
			get { return timeout; }
		}

		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot Color
		{
			get { return inColor; }
		}
		
		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot Size
		{
			get { return size; }
		}
		
		
		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot Position
		{
			get { return position; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Shape
		{
			get { return shape; }
		}
		
	}
}
